package com.lulu.baselib.share

import android.content.ContentValues.TAG
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.lulu.baselib.util.ImageUtil
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.io.ByteArrayOutputStream
import java.util.concurrent.CountDownLatch

/**
 * Author: XuChang
 * Date: 2020/9/21 22:10
 * Email: 123286473@qq.com
 * Description:
 * History:
 */
@RunWith(AndroidJUnit4::class)
class ShareTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        Assert.assertEquals("com.lulu.baselib", appContext.packageName)
    }

    var imgUrl =
        "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600707901148&di=5198c1e67be855628977619008bad75f&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F86%2F10%2F01300000184180121920108394217.jpg";

    fun log(msg: String) {
        Log.d("ShareTest", msg)
    }

    @Test
    fun loadImg() {
        log("start123123")
        val countDown = CountDownLatch(1)
        var bm: Bitmap? = null
        ImageUtil.loadImage(imgUrl, object : ImageUtil.LoadImageCallback {
            override fun onLoadSuccess(bitmap: Bitmap?) {
                val byteCount = bitmap?.byteCount
                log("byteCount = $byteCount")

                var out = ByteArrayOutputStream()
                bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, out)
                log("bytes byteCount = ${out.size()}")

                bm = bitmap!!;
                countDown.countDown()
            }

            override fun onLoadFailed(msg: String?) {
                log("request failed:$msg")
            }

        })
        countDown.await()
        log("finish")

        Assert.assertNull(bm)   //failed
    }

    /**
     * 测试图片压缩，指定的图片，要压缩到特定比例
     */
    @Test
    fun compressImage() {

    }
}